import router from "../../router";
import {
  Dialog
} from "vant";
export default {
  install(Vue, options) {
    // // 获取商品列表
    // Vue.prototype.GoodsGoodsList = function(){
    //   this.request.post('qrcapi/Goods_goodsList',{
    //     sessionId: userList.sessionId,
    //     userId: userList.id,
    //       // goodsCategoryId //不传为全部商品
    //     //  saleStatus//-1,-2 强制下架   0：下架  1：上架
    //     //  pageNo //页码 [一页10条]
    //   }).then(res=>{
    //     console.log(res,'获取商品列表')
    //   })
    // }

    //跳转下单页面
    (Vue.prototype.goConfirmOrder = function (
      pkey,
      goodsPa,
      item = {},
      referrer
    ) {
      //pkey 商品pkey goodsPa 商品规格 item商品信息 referrer 分享人
      if (item.tips) {
        Dialog.confirm({
            title: "立即购买须知",
            message: item.tips
          })
          .then(() => {

            router.push({
              path: '/coupon/confirmOrder',
              query: {
                pkey: pkey,
                goodsPa: goodsPa,
                referrer: referrer
              },
            });
          })
          .catch(() => {
            // on cancel
          });
      } else {
        router.push({
          path: '/coupon/confirmOrder',
          query: {
            pkey: pkey,
            goodsPa: goodsPa,
          },
        });
      }
    }),
    (Vue.prototype.goCouponDetail = function (
      pkey,
      goodsPa,
      item = {},
      referrer
    ) {
      router.push({
        path: "/coupon/detail",
        query: {
          pkey: pkey
        }
      });
      //pkey 商品pkey goodsPa 商品规格 item商品信息
      // if (item.tips) {
      //   Dialog.confirm({
      //       title: "立即购买须知",
      //       message: item.tips
      //     })
      //     .then(() => {
      //       router.push({
      //         path: "/coupon/detail",
      //         query: {
      //           pkey: pkey
      //         }
      //       });
      //     })
      //     .catch(() => {
      //       // on cancel
      //     });
      // } else {
      //   router.push({
      //     path: "/coupon/detail",
      //     query: {
      //       pkey: pkey
      //     }
      //   });
      // }
    }),
    (Vue.prototype.guid = function () {
      return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
        var r = (Math.random() * 16) | 0,
          v = c == "x" ? r : (r & 0x3) | 0x8;
        return v.toString(16);
      });
    });
    //把对象里的尾缀为Json的键值对  数组转换为JSON
    //type toJson 转换成JSON toString 转换成string
    Vue.prototype.Json_Array = function (jsonObject, type) {
      console.log("jsonObject", jsonObject);
      for (var item in jsonObject) {
        if (jsonObject.hasOwnProperty(item)) {
          if (item.indexOf("Json") != -1) {
            //判断是否为JSON键值对

            if (type == "toString") {
              //数组转字符串
              if (typeof jsonObject[item] == "object") {
                //判断是否为数组 对象
                jsonObject[item] = JSON.stringify(jsonObject[item]);
              }
            } else {
              //字符串转数组
              if (typeof jsonObject[item] == "string") {
                jsonObject[item] = JSON.parse(jsonObject[item]);
              } else {
                jsonObject[item] = [];
              }
            }
          }
        }
      }
      console.log(jsonObject);
      return jsonObject;
    };
    Vue.prototype.jump = function (adress, query) {
      this.$router.push({
        path: adress,
        query: query
      });
    };
    //导出下载
    Vue.prototype.downLoad = function (response) {
      console.log(response);
      let filename = decodeURI(
        response.headers["content-disposition"].split(";")[1].split("=")[1]
      );
      let blob = new Blob([response.data]);
      let url = window.URL.createObjectURL(blob);
      let a = document.createElement("a");
      a.setAttribute("href", url);
      a.download = filename;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    };
    //复制数据并取消关联性
    Vue.prototype.cloneObjectFn = function (obj) {
      // console.log(obj)
      if (obj) {
        return JSON.parse(JSON.stringify(obj));
      }
    };
    //表单验证
    Vue.prototype.watchLogin = function () {
      var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){7,19}$/;

      var regPhone = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
      var phoneNumber = this.phoneNumber;
      var passWord = this.passWord;
      if (reg.test(passWord) && this.check && regPhone.test(phoneNumber)) {
        this.successButton = false;
      } else {
        this.successButton = true;
      }
    };
    //当前年月日
    Vue.prototype.nowDate = function () {
      var myDate = new Date();
      var nowDate = `${myDate.getFullYear()}-${myDate.getMonth() +
        1}-${myDate.getDate()}`;
      return nowDate;
    };
    // 保存cookie
    Vue.prototype.setCookie = function (cName, value, expiredays) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate() + expiredays);
      document.cookie =
        cName +
        "=" +
        decodeURIComponent(value) +
        (expiredays == null ? "" : ";expires=" + exdate.toGMTString()) +
        ";path=/";
    };
    // 获取cookie
    Vue.prototype.getCookie = function (key) {
      if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(key + "=");
        if (start !== -1) {
          start = start + key.length + 1;
          var end = document.cookie.indexOf(";", start);
          if (end === -1) end = document.cookie.length;
          return unescape(document.cookie.substring(start, end));
        }
      }
      return "";
    };
    // 储存表单信息
    Vue.prototype.setUserInfo = function () {
      var that = this;
      // 判断用户是否勾选记住密码，如果勾选，向cookie中储存登录信息，
      // 如果没有勾选，储存的信息为空
      if (that.isRemember) {
        that.setCookie("account", that.phoneNumber);
        // base64加密密码
        that.setCookie("password", that.passWord);
        that.setCookie("remember", that.isRemember);
      } else {
        that.setCookie("account", "");
        that.setCookie("password", "");
      }
    };
  }
};